package com.lxb.myforum.service

import com.lxb.myforum.base.BaseService
import java.util.Date

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Service

import com.lxb.myforum.pojo.Post
import com.lxb.myforum.repository.PostRepository
import com.lxb.myforum.service.spec.PostSpec

@Service
class PostService @Autowired constructor(private val postRepository: PostRepository): BaseService() {

    fun findPosts(post: Post, pageable: PageRequest): Page<Post> {
        return postRepository.findAll(PostSpec(post), pageable)
    }

    fun savePost(post: Post): Post {
        if (post.id == null) {
            post.createTime = Date()
        }
        post.updateTime = Date() // 更新时间一直在变的
        return postRepository.save(post)
    }

    fun findById(id: Long): Post {
        return postRepository.findById(id).orElse(null)
    }
}
